package com.zhulanhua.blogs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhulanhua.blogs.dao.CommentDao;
import com.zhulanhua.blogs.domain.Comment;
import com.zhulanhua.blogs.domain.query.CommentQuery;
import com.zhulanhua.blogs.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentDao commentDao;

    @Override
    public List<Comment> getOk(long id) {
        CommentQuery cq = new CommentQuery();
        cq.setComments(true);
        cq.setExamine(1);
        cq.setSort("asc,create_time");
        return getQuery(cq);
    }

    @Override
    public int getCount(int id) {
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.select("count(*) as len");
        qw.lambda().eq(Comment::getArticleId, id);
        List<Map<String, Object>> maps = commentDao.selectMaps(qw);
        Object len = maps.get(0).get("len");
        return Integer.parseInt(String.valueOf(len));
    }

    @Override
    public List<Comment> getNo(int examine) {
        CommentQuery cq = new CommentQuery();
        cq.setExamine(examine);
        cq.setSort("asc,create_time");
        return getQuery(cq);
    }

    @Override
    public List<Comment> getQuery(CommentQuery commentQuery){
        QueryWrapper<Comment> qw = new QueryWrapper<>();

        //查询评论
        qw.lambda()
                .eq(Comment::getExamine, commentQuery.getExamine())
                .eq(commentQuery.getAid() != 0, Comment::getArticleId, commentQuery.getAid())
                .eq(commentQuery.isComments(), Comment::getCommentReply, 0);

        //排序
        String[] sort = commentQuery.getSort().split(",");
        if (sort != null){
            qw.orderByAsc(sort[0] == "asc", commentQuery.getList());
            qw.orderByDesc(sort[0] == "desc", commentQuery.getList());
        }

        //查询
        List<Comment> comments = commentDao.selectList(qw);

        if (commentQuery.isComments()){
            //查询回复
            for (Comment comment : comments){
                QueryWrapper<Comment> qwe = new QueryWrapper<>();
                qwe.lambda()
                        .eq(Comment::getExamine, commentQuery.getExamine())
                        .eq(Comment::getArticleId, commentQuery.getAid())
                        .eq(Comment::getCommentReply, comment.getCommentId());
                //排序
                if (sort != null){
                    qwe.orderByAsc(sort[0] == "asc", commentQuery.getList());
                    qwe.orderByDesc(sort[0] == "desc", commentQuery.getList());
                }

                //查询
                comment.setComments(commentDao.selectList(qwe));
            }
        }

        return comments;
    }

    @Override
    public boolean save(Comment comment) {
        return commentDao.insert(comment) == 1;
    }

    @Override
    public boolean delete(long id) {
        return commentDao.deleteById((int) id) == 1;
    }

    @Override
    public boolean update(long id, int examine) {
        Comment f = new Comment();
        f.setExamine(examine);
        f.setCommentId(id);
        return commentDao.updateById(f) == 1;
    }

    @Override
    public boolean praise(long id, boolean type) {
        Comment comment = new Comment();
        comment.setArticleId((int) id);
        Comment a = commentDao.selectById(id);
        if (type){
            comment.setSupport(a.getSupport() + 1);
        }else {
            comment.setSupport(a.getSupport() - 1);
        }
        return commentDao.updateById(comment) == 1;
    }
}
